import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView/HomeView.vue'
import HotView from "../views/HotView/HotView.vue"
import SelectView from "../views/SelectView/SelectView.vue"
import WatchView from "../views/WatchView/WatchView.vue"
import BackgroundView from "../views/BackgroundView/BackgroundView"
import LoveView from "../views/BackgroundView/Children/LoveView.vue"
import AroundView from "../views/BackgroundView/Children/AroundView.vue"
import ScienceView from "../views/BackgroundView/Children/ScienceView.vue"
import SupernaturalView from "../views/BackgroundView/Children/SupernaturalView.vue"
import CatoonView from "../../src/views/CatoonView.vue"
import LoginView from "../views/User/LoginView"
import UserView from "../views/User/UserView"

const pathArr = ['/home', '/user/:name','/user']


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/user', redirect: '/user/:name' },
  { path: '/login', component: LoginView },

  {
    path: '/user/:name', name: 'user',component: UserView, meta: {
      index: 10,
    },
  },
  {
    path: '/watch',
    name: 'watch',
    component: WatchView,
    children: [
      {
        path: 'watchstory',
        name: 'watchstory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'watchcontent',
            name: 'watchcontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 3,
    },


  },
  {
    path: '/hot',
    name: 'hot',
    component: HotView,
    children: [
      {
        path: 'hotstory',
        name: 'hotstory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'hotcontent',
            name: 'hotcontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 2,
    },
  },
  {
    path: '/select',
    name: 'select',
    component: SelectView,
    children: [
      {
        path: 'selectstory',
        name: 'selectstory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'selectcontent',
            name: 'selectcontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 4,
    },

  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: 'homestory',
        name: 'homestory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'homecontent',
            name: 'homecontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 0,
    },

  },

  {
    path: '/background',
    name: 'background',
    component: BackgroundView,

    children: [
      {
        path: 'backgroundstory',
        name: 'backgroundstory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'backgroundcontent',
            name: 'backgroundcontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 5,
    },
  },

  {
    path: '/around',
    name: 'around',
    component: AroundView,
    children: [
      {
        path: 'aroundstory',
        name: 'aroundstory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'aroundcontent',
            name: 'aroundcontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 8,
    },
  },

  {
    path: '/science',
    name: 'science',
    component: ScienceView,
    children: [
      {
        path: 'sciencestory',
        name: 'sciencestory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'sciencecontent',
            name: 'sciencecontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 9,
    },
  },

  {
    path: '/love',
    name: 'love',
    component: LoveView,
    children: [
      {
        path: 'lovestory',
        name: 'lovestory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'lovecontent',
            name: 'lovecontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 6,
    },
  },
  {
    path: '/supernatural',
    name: 'supernatural',
    component: SupernaturalView,
    children: [
      {
        path: 'supernaturalstory',
        name: 'supernaturalstory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'supernaturalcontent',
            name: 'supernaturalcontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 7,
    },
  },
  {
    path: '/catoon',
    name: 'catoon',
    component: CatoonView,
    children: [
      {
        path: 'catoonstory',
        name: 'catoonstory',
        component: () => import("../views/HomeView/HomeStoryView.vue"),
        children: [
          {
            path: 'catooncontent',
            name: 'catooncontent',
            component: () => import("../views/ContentView.vue"),
          }
        ]
      }
    ],
    meta: {
      index: 1,
    },
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(function (to, from, next) {
  if (pathArr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})


export default router
